# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def array_rank(arr)
    rank_array = arr
    ranks = (0..arr.length-1).to_a.sort_by{ |x| arr[x] }
    rank = 1
    rank_array = Array.new
    ranks.each do |r|
      rank_array[r] = rank
      rank += 1
    end
    ranks.each_with_index do |r,i|
      val = arr[r]
      for loop in i+1..arr.length-1 do
        if arr[ranks[loop]]==val
          rank_array[ranks[loop]] = rank_array[ranks[i]]
        end
      end
    end
    return rank_array
  end

  def array_rank_desc(arr)
    rank_array = arr
    ranks = (0..arr.length-1).to_a.sort_by{ |x| arr[x] }
    ranks = ranks.reverse
    rank_array = Array.new
    ranks.each_with_index do |r,i|
      rank_array[r] = i+1
    end
    ranks.each_with_index do |r,i|
      val = arr[r]
      for loop in i+1..arr.length-1 do
        if arr[ranks[loop]]==val
          rank_array[ranks[loop]] = rank_array[ranks[i]]
        end
      end
    end
    return rank_array
  end
end
